
# coding:utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import traceback

smtpserver = 'smtp.qq.com'
username = '1154238897@qq.com'
password = 'eltficvppincidcf'

class Mail:
    def __init__(self, date, to_email, content, sh, have):
        # 第三方 SMTP 服务
        self.date = date
        self.mail_host = "smtp.qq.com"  # 设置服务器:这个是qq邮箱服务器，直接复制就可以
        self.mail_pass = password  # 刚才我们获取的授权码
        self.sender = username  # 你的邮箱地址
        self.content = content
        self.receivers = [to_email,]  # 收件人的邮箱地址，可设置为你的QQ邮箱或者其他邮箱，可多个
        self.sh = sh
        self.have = have


    def send(self):
        content = '<p style="font-size:18px; ">' \
                  '{}，{}<br>-------------------<br><br>' \
                  '持仓情况：<br>{}<br>-------------------<br><br>' \
                  '机会：低于5%的有:' \
                  '<br>' \
                  '{}' \
                  '</p>'.format(self.date, self.sh, self.have, self.content)
        message = MIMEText(content, 'html', 'utf-8')

        message['From'] = Header("股票机会订阅", 'utf-8')

        subject = '股价百分位机会-订阅'  # 发送的主题，可自由填写
        message['Subject'] = Header(subject, 'utf-8')
        try:
            smtpObj = smtplib.SMTP_SSL(self.mail_host, 465)
            smtpObj.login(self.sender, self.mail_pass)
            smtpObj.sendmail(self.sender, self.receivers, message.as_string())
            smtpObj.quit()
            print('邮件发送成功')
        except smtplib.SMTPException as e:
            print('邮件发送失败')
            traceback.print_exc()



if __name__ == '__main__':
    import random
    content = ''
    mail = Mail(date='date',
                content=content,
                to_email='1154238897@qq.com')
    mail.send()